//
//  SetViewController.m
//  SelectionForYou
//
//  Created by qianfeng on 15-6-12.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "SetViewController.h"
#import "SDImageCache.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
}


@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showUI];
}
- (void)showUI {
    //分组样式表格
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    //设置数据源
    _dataArray = [[NSMutableArray alloc] init];
    NSArray *pushArray = [[NSArray alloc] initWithObjects:@"推送设置",@"开启推送通知",@"开启关注通知",@"清除缓存", nil];
    [_dataArray addObject:pushArray];
    
    NSArray *infoArray = [[NSArray alloc] initWithObjects:@"推荐爱限免",@"官方推荐",@"官方微博",@"版本 V1.8.1",@"感谢使用爱限免应用", nil];
    [_dataArray addObject:infoArray];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArray[section]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIN=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIN];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIN];
        
    }
    NSString *str=_dataArray[indexPath.section][indexPath.row];
    cell.textLabel.text=str;
    
    
    if (indexPath.section==0&&(indexPath.row==1||indexPath.row==2)) {
        UISwitch *mySwitch=[[UISwitch alloc] initWithFrame:CGRectMake(250, 5, 60, 30)];
        mySwitch.on=NO;
        mySwitch.tag=1001+indexPath.row;
        [mySwitch addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:mySwitch];
    }
    
    return cell;
}
- (void)switchClick:(UISwitch *)switcht{
    switch (switcht.tag) {
        case 1002:
        {
        
        
        }
            break;
        case 1003:
        {
            
            
        }
            break;
            
        default:
            break;
    }

}
- (CGFloat)getCachSize {
    //爱限免 有两类 缓存  sdWebImage 和 首页
    //获取图片缓存大小
    NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
    //获取自定义缓存大小
    //用枚举器遍历 一个文件夹的内容
    //1.获取 文件夹枚举器
    //
    
    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
    __block NSUInteger count = 0;
    //2.遍历
    for (NSString *fileName in enumerator) {
        NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
        count += fileDict.fileSize;//自定义所有缓存大小
    }
    // 得到是字节  转化为M
    CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
    return totalSize;
}

//cell被选择
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        //0分区
        switch (indexPath.row) {
            case 3://清除缓存
            {
                UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.5fM缓存",[self getCachSize]] preferredStyle:UIAlertControllerStyleActionSheet];
                //增加按钮
                [sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
                    //删除
                    //删除两部分
                    //1.删除 sd 图片缓存
                    //先清除内存中的图片缓存
                    [[SDImageCache sharedImageCache] clearMemory] ;
                    //清除磁盘的缓存
                    [[SDImageCache sharedImageCache] clearDisk];
                    
                    //2.删除自己缓存
                    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
                    [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
                    
                    
                    
                }]];
                [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    //取消
                }]];
                //跳转
                [self presentViewController:sheet animated:YES completion:nil];
                
            }
                break;
                
            default:
                break;
        }
        
    }else {
        
            switch (indexPath.row) {
            case 0://邮箱推荐
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://673749102@qq.com"]];
                NSLog(@"邮箱推荐");
                
            }
                break;
            case 1://爱限免的AppStore地址(官方推荐)
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.guokr.com/"]];
                 NSLog(@"官网推荐");
            }
                break;
            case 2://官方微博
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://t.qq.com/zhugy781"]];
                 NSLog(@"微博推荐");
            }
                break;
                
            default:
                break;
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
